<?
include '../includes/specs_functions.php';
#------------------------------------------------------------------------
# Connect to PADS
$DB = getDB();
#-------------------------------$Session information
$SESSION = get_session($DB);
$CICUID = $SESSION->Character;
$THEME = $_POST["THEME"];
#---------------------------------------------------
#------------------------------------------------------------------------
# Connect to PADS
$DB = getDB();

$output = json_encode(array("status"=>"error", "message"=>"NO"));
	
$email = $_POST['email'];
$password = $_POST['password'];

$getXmlUrl = "http://www.ffxivspecs.com/xivpadsExport/export.php?action=export&email=".$email."&passw=".$password;

$specs = new SimpleXMLElement($getXmlUrl, null, true);

/* HANDLE XML AND INSERT IT */

$done = true;

if($CICUID != ""){

	$i = 0;

	foreach($specs as $spec)
	{
		$new_table = SPEC_TBL_SPECS;
		$new_data = array(SPEC_TBL_SPECS_USER_ID."=\"".$CICUID."\""
					,SPEC_TBL_SPECS_LAYOUT."=\"".$spec->layout."\""
				   ,SPEC_TBL_SPECS_JOB_ID."=\"".$spec->jobId."\""
				   ,SPEC_TBL_SPECS_LEVEL."=\"".$spec->level."\""
				   ,SPEC_TBL_SPECS_DESCRIPTION."=\"".$spec->description."\""
				   ,SPEC_TBL_SPECS_PUBLIC."=\"".$spec->public."\""
				   ,SPEC_TBL_SPECS_CDATE."=\"".$spec->cdate."\""
				   ,SPEC_TBL_SPECS_UDATE."=\"".$spec->udate."\"");

		$new_result = $DB->Insert($new_table ,$new_data);
		
		if ($new_result[0][1] != 0)
		{
			$done = false;
		}else{
			$i++;
		}
	}
	if ($done)
	{
		$output = json_encode(array("status"=>"success", "message"=>"Imported ".$i." specs!"));
	}
		
		
	$getXmlUrl = "http://www.ffxivspecs.com/xivpadsExport/export.php?action=confirm&email=".$email."&passw=".$password;
	$xml = file_get_contents($getXmlUrl);

	echo $output; 
}else{
	$output = json_encode(array("status"=>"success", "message"=>"No character on XIVPADS found, please link your account to a character!"));
}
#------------------------------------------------------------------------
# Disconnect
leaveDB($DB);

?>